//class LabelButton
package baidu.ui.controls
{
    import baidu.ui.constants.*;
    import baidu.ui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class LabelButton extends baidu.ui.controls.Button
    {
        public function LabelButton()
        {
            super();
            return;
        }

        public override function get classStyles():Object
        {
            return mergeStyles(super.classStyles, defaultStyles);
        }

        public override function get selected():Boolean
        {
            return this._toggle ? _selected : false;
        }

        public override function set selected(arg1:Boolean):void
        {
            _selected = arg1;
            if (this._toggle)
            {
                invalidate(baidu.ui.core.Invalidation.STATE);
            }
            return;
        }

        public function get icon():Object
        {
            return getStyle("iconSkin");
        }

        public function set icon(arg1:*):void
        {
            setStyle("iconSkin", arg1);
            return;
        }

        public function get label():String
        {
            return this._label;
        }

        public function set label(arg1:String):void
        {
            this._label = arg1;
            if (this.textField.text != this._label)
            {
                this.textField.text = this._label;
            }
            invalidate(baidu.ui.core.Invalidation.SIZE);
            invalidate(baidu.ui.core.Invalidation.STYLES);
            return;
        }

        public function get labelPlacement():String
        {
            return this._labelPlacement;
        }

        public function set labelPlacement(arg1:String):void
        {
            this._labelPlacement = arg1;
            invalidate(baidu.ui.core.Invalidation.SIZE);
            return;
        }

        public function get toggle():Boolean
        {
            return this._toggle;
        }

        public function set toggle(arg1:Boolean):void
        {
            if (!arg1 && super.selected)
            {
                this.selected = false;
            }
            this._toggle = arg1;
            if (this._toggle)
            {
                addEventListener(flash.events.MouseEvent.CLICK, this.toggleSelected, false, 0, true);
            }
            else
            {
                removeEventListener(flash.events.MouseEvent.CLICK, this.toggleSelected);
            }
            invalidate(baidu.ui.core.Invalidation.STATE);
            return;
        }

        public function get value():Object
        {
            return this._value;
        }

        public function set value(arg1:Object):void
        {
            this._value = arg1;
            return;
        }

        public function get autoSize():Object
        {
            return this._autoSize;
        }

        public function set autoSize(arg1:Object):void
        {
            this._autoSize = arg1;
            invalidate(baidu.ui.core.Invalidation.SIZE);
            return;
        }

        protected function toggleSelected(arg1:flash.events.MouseEvent):void
        {
            this.selected = !this.selected;
            dispatchEvent(new flash.events.Event(flash.events.Event.CHANGE, true));
            return;
        }

        protected override function initUI():void
        {
            super.initUI();
            this.textField = new flash.text.TextField();
            this.textField.type = flash.text.TextFieldType.DYNAMIC;
            this.textField.selectable = false;
            addChild(this.textField);
            return;
        }

        protected override function draw():void
        {
            if (this.textField.text != this._label)
            {
                this.label = this._label;
            }
            if (isInvalid(baidu.ui.core.Invalidation.STYLES, baidu.ui.core.Invalidation.STATE))
            {
                drawBackground();
                this.drawIcon();
                this.drawTextFormat();
                invalidate(baidu.ui.core.Invalidation.SIZE, false);
            }
            if (isInvalid(baidu.ui.core.Invalidation.SIZE))
            {
                this.drawLayout();
            }
            validate();
            return;
        }

        protected function drawIcon():void
        {
            var loc2:*;
            loc2 = null;
            var loc1:*;
            loc1 = this._icon;
            this._icon = getSkinInstance(getStyleValue("iconSkin")) as flash.display.DisplayObject;
            if (this._icon != null)
            {
                addChildAt(this._icon, 1);
                if (this._icon as flash.display.MovieClip)
                {
                    loc2 = enabled ? mouseState : "disabled";
                    if (this.selected)
                    {
                        loc2 = "selected" + loc2.substr(0, 1).toUpperCase() + loc2.substr(1);
                    }
                    (this._icon as flash.display.MovieClip).gotoAndStop(frameDict[loc2]);
                }
            }
            if (!(loc1 == null) && !(loc1 == this._icon))
            {
                removeChild(loc1);
            }
            return;
        }

        protected function drawTextFormat():void
        {
            var loc1:*;
            loc1 = getStyleValue(enabled ? "textFormat" : "disabledTextFormat") as flash.text.TextFormat;
            if (loc1 != null)
            {
                this.textField.setTextFormat(loc1);
                this.textField.defaultTextFormat = loc1;
            }
            loc1 = getStyleValue("overTextFormat") as flash.text.TextFormat;
            if (mouseState == "over" || mouseState == "down" || this.selected)
            {
                if (loc1 != null)
                {
                    this.textField.setTextFormat(loc1);
                    this.textField.defaultTextFormat = loc1;
                }
            }
            this.setEmbedFont();
            return;
        }

        protected function setEmbedFont():void
        {
            var loc1:*;
            loc1 = getStyleValue("embedFonts");
            if (loc1 != null)
            {
                this.textField.embedFonts = loc1;
            }
            return;
        }

        protected override function drawLayout():void
        {
            var loc10:*;
            loc10 = NaN;
            var loc11:*;
            loc11 = NaN;
            var loc1:*;
            loc1 = Number(getStyleValue("padding"));
            var loc2:*;
            loc2 = Number(getStyleValue("gap"));
            var loc3:*;
            loc3 = this._icon != null ? this._labelPlacement : baidu.ui.constants.BUIConstants.TOP;
            this.textField.height = this.textField.textHeight;
            var loc4:*;
            loc4 = this.textField.textWidth;
            var loc5:*;
            loc5 = this.textField.textHeight;
            var loc6:*;
            loc6 = this._icon != null ? this._icon.width : 0;
            var loc7:*;
            loc7 = this._icon != null ? this._icon.height : 0;
            var loc8:*;
            loc8 = this._icon != null ? this._icon.width + loc2 : 0;
            var loc9:*;
            loc9 = this._icon != null ? this._icon.height + loc2 : 0;
            this.textField.visible = this.label.length > 0;
            if (this.autoSize)
            {
                this.textField.autoSize = "left";
                if (loc3 == baidu.ui.constants.BUIConstants.BOTTOM || loc3 == baidu.ui.constants.BUIConstants.TOP)
                {
                    _width = Math.max(loc4, loc6) + loc1 * 2;
                    _height = loc5 + loc9 + loc1 * 2;
                }
                else
                {
                    _width = loc4 + loc8 + loc1 * 2;
                    _height = Math.max(loc5, loc7) + loc1 * 2;
                }
            }
            else
            {
                this.textField.autoSize = "none";
            }
            if (this._icon != null)
            {
                this._icon.x = Math.round((_width - this._icon.width) / 2);
                this._icon.y = Math.round((_height - this._icon.height) / 2);
            }
            if (this.textField.visible != false)
            {
                if (loc3 == baidu.ui.constants.BUIConstants.BOTTOM || loc3 == baidu.ui.constants.BUIConstants.TOP)
                {
                    loc10 = Math.max(0, Math.min(loc4, _width - 2 * loc1));
                    loc11 = Math.min(_height - 2, loc5);
                    var loc12:*;
                    loc4 = loc12 = loc10;
                    this.textField.width = loc12;
                    loc5 = loc12 = loc11;
                    this.textField.height = loc12;
                    this.textField.x = Math.round((_width - loc4) / 2);
                    this.textField.y = Math.round((_height - this.textField.height - loc9) / 2 + (loc3 != baidu.ui.constants.BUIConstants.BOTTOM ? 0 : loc9));
                    if (this._icon != null)
                    {
                        this._icon.y = Math.round(loc3 != baidu.ui.constants.BUIConstants.BOTTOM ? this.textField.y + this.textField.height + loc2 : this.textField.y - loc9);
                    }
                }
                else
                {
                    loc10 = Math.max(0, Math.min(loc4, _width - loc8 - 2 * loc1));
                    loc4 = loc12 = loc10;
                    this.textField.width = loc12;
                    this.textField.x = Math.round((_width - loc4 - loc8) / 2 + (loc3 == baidu.ui.constants.BUIConstants.LEFT ? 0 : loc8));
                    this.textField.y = Math.round((_height - this.textField.height) / 2);
                    if (this._icon != null)
                    {
                        this._icon.x = Math.round(loc3 == baidu.ui.constants.BUIConstants.LEFT ? this.textField.x + loc4 + loc2 : this.textField.x - loc8);
                    }
                }
            }
            else
            {
                this.textField.width = 0;
                this.textField.height = 0;
            }
            super.drawLayout();
            return;
        }


        {
            defaultStyles = {"iconSkin":"Button_Icon_Skin", "padding":5, "gap":5, "embedFonts":false};
        }

        protected var _value:Object;

        public var textField:flash.text.TextField;

        protected var _labelPlacement:String="right";

        protected var _toggle:Boolean=false;

        protected var _icon:flash.display.DisplayObject;

        protected var _label:String="Label";

        protected var _autoSize:Object=false;

        public static var defaultStyles:Object;
    }
}


